using OneDayModel.Interfaces;

namespace OneDayModel.Model
{
    public class AlbumsWorksModel
    {
        private readonly IRepository repository;
        private readonly IFileSystem fileSystem;

        public AlbumsWorksModel(IRepository repository, IFileSystem fileSystem)
        {
            this.repository = repository;
            this.fileSystem = fileSystem;
        }

        public IAlbum CreateAlbum(string albumName)
        {
            IUser user = SessionContext.GetInstance().User;

            IAlbum registeredAlbum = repository.RegisterAlbum(user, albumName);
            user.AddAlbum(registeredAlbum);
            return registeredAlbum;
        }

        public IAlbum GetAlbumForLoggedInUser(string albumName)
        {
            IUser user = SessionContext.GetInstance().User;
            return user.GetAlbum(albumName);
        }

        public string AddPictureToAlbum(string albumName, IUploadedFile uploadedFile)
        {
            IUser user = SessionContext.GetInstance().User;
            string userAlias = user["Alias"];

            string albumPath = repository.GetBasePathForAlbums(userAlias);
            albumPath = fileSystem.CombinePath(albumPath, albumName);
            string thumbsPath = fileSystem.CombinePath(albumPath, "Thumbs");

            if (!fileSystem.FolderExists(albumPath))
                fileSystem.CreateFolder(albumPath);

            if (!fileSystem.FolderExists(thumbsPath))
                fileSystem.CreateFolder(thumbsPath);

            string fileName = uploadedFile.SavePictureAndThumb(fileSystem, albumPath, thumbsPath);
            if (fileName != string.Empty)
            {
                IAlbum album = GetAlbumForLoggedInUser(albumName);
                repository.RegisterPictureInAlbum(userAlias, albumName, fileName);
                album.AddPicture(fileName); //???
            }

            return fileName;
        }
    }
}